﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using ScrumTable.Common;
using ScrumTable.Common.Threading;

#endregion

namespace ScrumTable.DL.Data.Common
{
    /// <summary>
    /// This class contains a default implementation of the ICancellableDataResult
    /// interface.
    /// </summary>
    public class DefaultCancellableDataResult : DataResult<bool>, ICancellableDataResult
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly Action _cancelAction;
        private volatile bool _canceled = false;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Returns true, if the changes have been accepted.
        /// </summary>
        public bool DataAccepted
        {
            get { return Data; }
        }

        /// <summary>
        /// Returns true, if the event (request) has been canceled.
        /// </summary>
        public bool Canceled
        {
            get { return _canceled; }
        }

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new DefaultCancellableDataResult instance with the specified data.
        /// </summary>
        /// <param name="cancelAction">Specifies the cancel action.</param>
        /// <param name="accepted">True if the changes have been accepted.</param>
        public DefaultCancellableDataResult(Action cancelAction, bool accepted)
            : base(accepted)
        {
            _cancelAction = cancelAction;
        }

        /// <summary>
        /// Creates a new DefaultCancellableDataResult instance with the specified data.
        /// </summary>
        /// <param name="accepted">True if the changes have been accepted.</param>
        public DefaultCancellableDataResult(bool accepted)
            : base(accepted)
        {
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Aborts the stop process so the data layer tries to update the
        /// data with the saver.
        /// </summary>
        public void Cancel()
        {
            _canceled = true;

            if (_cancelAction != null)
                _cancelAction();
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}